home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / cc12.c < prev    next >
Text File  |  1980-01-01  |  11KB  |  380 lines

  1.  
  2.  
  3. /*
  4. ** open an include file
  5. */
  6. doinclude()  {
  7.   char c, *fname, buff[15];
  8.   int i;
  9.  
  10.   blanks();       /* skip over to name */
  11.   /*
  12.    * added code to handle include filename in quotes or brackets
  13.    * 4/5/83 br
  14.    */
  15.   if ((*lptr == '"') | (*lptr == '<')) {
  16.     i = 0;
  17.     fname = buff;
  18.     while (i<14) {
  19.       c = *++lptr;
  20.       if ((c == '"') | (c == '>'))
  21.         i = 14; /* force exit from loop */
  22.       else
  23.         *fname++ = c;
  24.       }
  25.     *fname = '\0';
  26.     fname = buff;
  27.     }
  28.   else
  29.     fname = lptr;       /* no '"' or '<' (original convention) */
  30.  
  31.    if(inclevel <= 5){                    /* fas 2.7 */
  32.      if((input2[++inclevel]=fopen(fname,"r"))==NULL) {    /* fas 2.7 */
  33.        input2[inclevel--]= EOF;                /* fas 2.7 */
  34.        error("open failure on include file");        /* fas 2.7 */
  35.        }                        /* fas 2.7 */
  36.    }else{                        /* fas 2.7 */
  37.       error("maximum include nesting reached");        /* fas 2.7 */
  38.    }                            /* fas 2.7 */
  39.  
  40.   kill();         /* clear rest of line */
  41.       /* so next read will come from */
  42.       /* new file (if open) */
  43.   }
  44.  
  45.  
  46. /*
  47. ** test for global declarations
  48. */
  49. dodeclare(class) int class; {
  50.   if(amatch("char",4)) {
  51.     declglb(CCHAR, class);
  52.     ns();
  53.     return 1;
  54.     }
  55.   else if((amatch("int",3))|(class==EXTERNAL)) {
  56.     declglb(CINT, class);
  57.     ns();
  58.     return 1;
  59.     }
  60.   return 0;
  61.   }
  62.  
  63. /*
  64. ** delcare a static variable
  65. */
  66. declglb(type, class)  int type, class; {
  67.   int k, j, ll, aindex, i, ii, jj, temp[8];
  68.                         /* fas 2.3, 2.6 */
  69.   char arrindex;                /* fas 2.6 */
  70.   itype = type;                    /* fas 2.3 */
  71.   while(1) {
  72.     arrindex = 0;                /* fas 2.6 */
  73.     ii = jj = level = i = 0;            /* fas 2.3, 2.6 */
  74.     if(endst()) return;     /* do line */
  75.     if(match("(*")|match("*")) {                        /*03*/
  76.       j=POINTER;
  77.       k=0;
  78.       while(match("*"))level++;            /* fas 2.3 */
  79.       if(level) type = CINT;            /* fas 2.3 */
  80.       }
  81.     else {
  82.       j=VARIABLE;
  83.       k=1;
  84.       }
  85.     if (symname(ssname, YES)==0) illname();
  86.     if(findglb(ssname)) multidef(ssname);
  87.     if(match(")")) ;               /*03*/
  88.     if(match("()")) j=FUNCTION;
  89.     else if (match("[")) {
  90.       ll = level;                /* fas 2.5 */
  91.       k=needsub();    /* get size  */
  92.       level = ll;                /* fas 2.5 */
  93.       if(j != POINTER){                /* fas 2.5 */
  94.         j=ARRAY;                /* fas 2.5 */
  95.         while(match("[")){            /* fas 2.6 */
  96.           temp[i] = needsub();            /* fas 2.6 */
  97.           if(++i >= MAXELMNTS){            /* fas 2.6 */
  98.              error("max md elements exceeded");    /* fas 2.6 */
  99.              k = 0;                /* fas 2.6 */
  100.              }                    /* fas 2.6 */
  101.           k *= temp[i-1];            /* fas 2.6 */
  102.           }
  103.         if(k>0 && i>0){                /* fas 2.6 */
  104.           if(arrcount <= MAXARRAYS){        /* fas 2.6 */
  105.             arrptr += ARRINDEX;            /* fas 2.6 */
  106.             arrindex = arrcount++;        /* fas 2.6 */
  107.           }else{                /* fas 2.6 */
  108.             error("max md arrays exceeded");    /* fas 2.6 */
  109.             k = 0;                /* fas 2.6 */
  110.           }
  111.           for(jj = 0; jj < i; jj++){        /* fas 2.6 */
  112.              arrptr[jj] = 1;            /* fas 2.6 */
  113.              for(ii = i-1; ii >= jj; ii--)    /* fas 2.6 */
  114.                 arrptr[jj] *= temp[ii];        /* fas 2.6 */
  115.           } 
  116.         }
  117.         }else{                    /* fas 2.5 */
  118.            type = CINT;                /* fas 2.5 */
  119.            ++level;                /* fas 2.5 */
  120.         }                    /* fas 2.5 */
  121.       }
  122.     if(class==EXTERNAL) external(ssname);
  123.     else if(j!=FUNCTION) j=initials(type>>2, j, k);     /*16*/
  124.     addsym(ssname, j, type, level, itype, arrindex, k, &glbptr, class);
  125.                         /* fas 2.3 2.6 */
  126.     if (match(",")==0) return; /* more? */
  127.     }
  128.   }
  129.  
  130. /*
  131. ** declare local variables
  132. */
  133. declloc(typ)  int typ;  {
  134.   int k, j, l;                        /* fas 2.5 */
  135.   itype = typ;                        /* fas 2.3 */
  136.   if(swactive) error("not allowed in switch");         /*08*/
  137. #ifdef STGOTO
  138.   if(noloc) error("not allowed with goto");
  139. #endif
  140.   if(declared < 0) error("must declare first in block");
  141.   while(1) {
  142.     level = 0;                        /* fas 2.3 */
  143.     while(1) {
  144.       if(endst()) return;
  145.       if(match("*")){
  146.         j=POINTER;
  147.         while(match("*")) level++;            /* fas 2.3 */
  148.         if(level) typ = CINT;                /* fas 2.3 */
  149.       }
  150.       else           j=VARIABLE;
  151.       if (symname(ssname, YES)==0) illname();
  152.       /* no multidef check, block-locals are together */
  153.       k=BPW;
  154.       if (match("[")) {
  155.         l = level;                    /* fas 2.5 */
  156.         if(k=needsub()) {          /*25*/
  157.           if(j != POINTER){                /* fas 2.5 */
  158.             j=ARRAY;                    /* fas 2.5 */
  159.           }else{                    /* fas 2.5 */
  160.             typ = CINT;                    /* fas 2.5 */
  161.             level = l;                    /* fas 2.5 */
  162.             }                        /* fas 2.5 */
  163.          
  164.           if(typ==CINT)k=k<<LBPW;
  165.           }
  166.         else {j=POINTER; k=BPW;}   /*25*/
  167.         }
  168.                                    /*14*/
  169.  
  170.       else if((typ==CCHAR)&(j==VARIABLE)) k=SBPC;
  171.       declared = declared + k;
  172.       addsym(ssname, j, typ, level, itype, 0,csp-declared, &locptr, AUTOMATIC);
  173.                          /* fas 2.3 2.6 */
  174.       break;
  175.       }
  176.     if (match(",")==0) return;
  177.     }
  178.   }
  179.  
  180. /*
  181. ** initialize global objects
  182. */
  183. initials(size, ident, dim) int size, ident, dim; {
  184.   int savedim;
  185.   litptr=0;
  186.   if(dim==0) dim = -1;
  187.   savedim=dim;
  188.   entry();
  189.   if(match("=")) {
  190.     if(match("{")) {
  191.       while(dim) {
  192.         init(size, ident, &dim);
  193.         if(match(",")==0) break;
  194.         }
  195.       needtoken("}");
  196.       }
  197.     else init(size, ident, &dim);
  198.     }
  199.   if((dim == -1)&(dim==savedim)) {
  200.      stowlit(0, size=BPW);
  201.     ident=POINTER;
  202.     }
  203.   dumplits(size);
  204.   dumpzero(size, dim);
  205.   return ident;
  206.   }
  207.  
  208. /*
  209. ** evaluate one initializer
  210. */
  211. init(size, ident, dim) int size, ident, *dim; {
  212.   int value;
  213.   if(qstr(&value)) {
  214.     if((ident==VARIABLE)|(size!=1))
  215.       error("must assign to char pointer or array");
  216.     *dim = *dim - (litptr - value);
  217.     if(ident==POINTER) point();
  218.     }
  219.   else if(constexpr(&value)) {
  220.     if(ident==POINTER) error("cannot assign to pointer");
  221.     stowlit(value, size);
  222.     *dim = *dim - 1;
  223.     }
  224.   }
  225.  
  226. /*
  227. ** get required array size
  228. */
  229. needsub()  {
  230.   int val;
  231.   if(match("]")) return 0; /* null size */
  232.   if (constexpr(&val)==0) val=1;
  233.   if (val<0) {
  234.     error("negative size illegal");
  235.     val = -val;
  236.     }
  237.   needtoken("]");      /* force single dimension */
  238.   return val;          /* and return size */
  239.   }
  240.  
  241. /*
  242. ** begin a function
  243. **
  244. ** called from "parse" and tries to make a function
  245. ** out of the following text
  246. **
  247. ** Patched per P.L. Woods (DDJ #52)
  248. */
  249. newfunc()  {
  250.   char *ptr;
  251. #ifdef STGOTO
  252.   nogo  =             /* enable goto statements */
  253.   noloc = 0;          /* enable block-local declarations */
  254. #endif
  255.   lastst=             /* no statement yet */
  256.   litptr=0;           /* clear lit pool */
  257.   arrcount = 0;                           /* fas 2.6 */
  258.   litlab=getlabel();  /* label next lit pool */
  259.   locptr=STARTLOC;    /* clear local variables */
  260.   if(monitor) lout(line, stderr);
  261.   if (symname(ssname, YES)==0) {
  262.     error("illegal function or declaration");
  263.     kill(); /* invalidate line */
  264.     return;
  265.     }
  266.   if(func1) {
  267.     postlabel(beglab);
  268.     func1=0;
  269.     }
  270.   if(ptr=findglb(ssname)) {      /* already in symbol table ? */
  271.     if(ptr[IDENT]!=FUNCTION)       multidef(ssname);
  272.     else if(ptr[OFFSET]==FUNCTION) multidef(ssname);
  273.     else {                    /*37*/
  274.       /* earlier assumed to be a function */
  275.       ptr[OFFSET]=FUNCTION;
  276.       ptr[CLASS]=STATIC;      /*37*/
  277.       }                       /*37*/
  278.     }
  279.   else
  280.     addsym(ssname, FUNCTION, CINT, 0, 0, 0, FUNCTION, &glbptr, STATIC);
  281.                         /* fas 2.3 2.6 */
  282.   if(match("(")==0) error("no open paren");
  283.   entry();
  284.   locptr=STARTLOC;
  285.   argstk=0;               /* init arg count */
  286.   while(match(")")==0) {  /* then count args */
  287.     /* any legal name bumps arg count */
  288.     if(symname(ssname, YES)) {
  289.       if(findloc(ssname)) multidef(ssname);
  290.       else {
  291.         addsym(ssname, 0, 0, 0, 0, 0,  argstk, &locptr, AUTOMATIC);
  292.                         /* fas 2.3 2.6 */
  293.         argstk=argstk+BPW;
  294.